/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.spring.controlador;

import com.spring.modelo.entidad.Aula;
import com.spring.modelo.entidad.Carrera;
import com.spring.modelo.entidad.Curso;
import com.spring.modelo.entidad.Docente;
import com.spring.modelo.entidad.Semestre;
import com.spring.modelo.entidad.Planes;
import com.spring.servicios.ServicioAula;
import com.spring.servicios.ServicioDocente;
import com.spring.servicios.ServicioSemestre;
import com.spring.servicios.servicioCarrera;
import com.spring.servicios.servicioCurso;
import com.spring.servicios.ServicioPlan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author karolyn
 */
@Controller
public class controlador {
      @Autowired
    private ServicioSemestre sdao;
      @Autowired
    private ServicioAula sadao;
    @Autowired
    private ServicioDocente sddao;
    @Autowired
    private servicioCurso cudao;
    @Autowired
    private servicioCarrera cadao;
    @Autowired
    private ServicioPlan pdao;
    
    @RequestMapping (value="/index.htm", method= RequestMethod.GET)
    public String Index(){
        return "index";
    }
    @RequestMapping (value="/Aula.htm", method= RequestMethod.GET)
    public String Aula(){
        return "Aula";
    }
    @RequestMapping (value="/Docente.htm", method= RequestMethod.GET)
    public String Docente(){
        return "Docente";
    }
    @RequestMapping (value="/Semestre.htm", method= RequestMethod.GET)
    public String Semestre(){
        return "Semestre";
    }
    @RequestMapping (value="/Curso.htm", method= RequestMethod.GET)
    public String Curso(){
        return "Curso";
    }
    @RequestMapping (value="/Carrera.htm", method= RequestMethod.GET)
    public String Carrera(){
        return "Carrera";
    }
    @RequestMapping (value="/Plan.htm", method= RequestMethod.GET)
    public String Plan(){
        return "Plan";
    }
    
    
    //Insertar
    @RequestMapping(value="/frmInsertarAula.htm", method= RequestMethod.GET)
    public ModelAndView InsertarAula(){
        ModelAndView mv = new ModelAndView("frmInsertarAula");
        Aula a = new Aula();
        a.setActivo(true);
        mv.addObject("IAula", a);
        return mv;
    }
    @RequestMapping(value="/frmInsertarDocente.htm", method= RequestMethod.GET)
    public ModelAndView InsertarDocente(){
        ModelAndView mv = new ModelAndView("frmInsertarDocente");
        Docente d = new Docente();
        d.setActivo(true);
        mv.addObject("IDocente", d);
        return mv;
    }
    @RequestMapping(value="/frmInsertarSemestre.htm",method=RequestMethod.GET)
    public ModelAndView InsertarSemestre(){
        ModelAndView mv=new ModelAndView("frmInsertarSemestre");
        Semestre s=new Semestre();
        s.setVigente(true);
        mv.addObject("SemestreBB",s);
        return mv;
    }
    @RequestMapping(value = "/frmInsertarCurso.htm", method = RequestMethod.GET)
    public ModelAndView InsertarCurso() {
        ModelAndView mv = new ModelAndView("frmInsertarCurso");
        Curso c = new Curso();
        c.setActivo(true);
        mv.addObject("cursoC", c);
        return mv;
    }
    
    @RequestMapping(value = "/frmInsertarCarrera.htm", method = RequestMethod.GET)
    public ModelAndView InsertarCarrera() {
        ModelAndView mv = new ModelAndView("frmInsertarCarrera");
        Carrera c = new Carrera();
        c.setActivo(true);
        mv.addObject("carreraC", c);
        return mv;
    }
    
    @RequestMapping(value = "/frmInsertarPlan.htm", method = RequestMethod.GET)
    public ModelAndView InsertarPlan() {
        ModelAndView mv = new ModelAndView("frmInsertarPlan");
        Planes p = new Planes();
        p.setVigente(true);
        mv.addObject("lista_carreras", cadao.getListaCarrera());
        mv.addObject("planP", p);
        return mv;
    }
    
    //Procesar Insercion
    @RequestMapping(value="/procesaInsertarAula.htm", method= RequestMethod.POST)
    public String InsertarAula(@ModelAttribute("IAula") Aula a){
        if(a.getActivo()==null)
            a.setActivo(true);
        else
            a.setActivo(false);
        return (sadao.insertarAula(a))?"exito":"error";
    }
    @RequestMapping(value="/procesaInsertarDocente.htm", method= RequestMethod.POST)
    public String InsertarDocente(@ModelAttribute("IDocente") Docente d){
        if(d.getActivo()==null)
            d.setActivo(true);
        else
            d.setActivo(false);
        return (sddao.insertarDocentes(d))?"exito":"error";
    }
    @RequestMapping(value="/procesaInsertarSemestre.htm", method= RequestMethod.POST)
    public ModelAndView procesaInsertarSemestre(@ModelAttribute("SemestreBB") Semestre se) {
        System.out.println(se.getVigente());
        boolean b = sdao.insertarSemestre(se) ;
        //System.out.println("sss--" + b);
        ModelAndView mv = new ModelAndView("exito");
        return mv;
    }
    @RequestMapping(value = "/procesaInsertarCurso.htm", method = RequestMethod.POST)
    public ModelAndView procesaInsertarCurso(@ModelAttribute("cursoC") Curso c) {
        System.out.println(c.getActivo());
        boolean b = cudao.insertarCurso(c);
        //System.out.println("sss--" + b);
        ModelAndView mv = new ModelAndView("exito");
        return mv;
    }

   @RequestMapping(value = "/procesaInsertarCarrera.htm", method = RequestMethod.POST)
    public ModelAndView procesaInsertarCarrera(@ModelAttribute ("carreraC") Carrera c) {
        System.out.println(c.getActivo());
        boolean b = cadao.insertarCarrera(c);
        //System.out.println("sss--" + b);
        ModelAndView mv = new ModelAndView("exito");
        return mv;
    }
    @RequestMapping(value = "/procesaInsertarPlan.htm", method = RequestMethod.POST)
    public ModelAndView procesaInsertarPlan(@ModelAttribute("planP") Planes p) {
        System.out.println(p.getVigente());
        boolean b = pdao.insertarPlan(p);
        //System.out.println("sss--" + b);
        ModelAndView mv = new ModelAndView("exito");
        return mv;
    }
    
    //Listar
    @RequestMapping(value = "/frmListarAula.htm", method = RequestMethod.GET)
    public ModelAndView frmListarAulas() {
        ModelAndView mv = new ModelAndView("frmListarAulas");
        mv.addObject("aulas", sadao.listarAulas());
        return mv;
    }
    @RequestMapping(value = "/frmListarDocentes.htm", method = RequestMethod.GET)
    public ModelAndView frmListarDocentes() {
        ModelAndView mv = new ModelAndView("frmListarDocentes");
        mv.addObject("docentes", sddao.listarDocentes());
        return mv;
    }
    @RequestMapping(value = "/frmListarSemestre.htm", method = RequestMethod.GET)
    public ModelAndView frmListarSemestres() {
        ModelAndView mv = new ModelAndView("frmListarSemestres");
        mv.addObject("semestres", sdao.ListarSemestre());
        return mv;
    }
    @RequestMapping(value = "/frmListarCarrera.htm", method = RequestMethod.GET)
    public ModelAndView frmListarCarreras() {
        ModelAndView mv = new ModelAndView("frmListarCarreras");
        mv.addObject("carreras", cadao.getListaCarrera());
        //mv.addObject("tipotrabajadorBB", new TipoTrabajador());
        return mv;
    }
    @RequestMapping(value = "/frmListarCurso.htm", method = RequestMethod.GET)
    public ModelAndView frmListarCursos() {
        ModelAndView mv = new ModelAndView("frmListarCursos");
        mv.addObject("cursos", cudao.getListaCurso());
        //mv.addObject("tipotrabajadorBB", new TipoTrabajador());
        return mv;
    }
    @RequestMapping(value = "/frmListarPlan.htm", method = RequestMethod.GET)
    public ModelAndView frmListadoPlanes() {
        ModelAndView mv = new ModelAndView("frmListarPlan");
        mv.addObject("planes", pdao.getListaPlan());
        //mv.addObject("tipotrabajadorBB", new TipoTrabajador());
        return mv;
    }
    
    //Buscar
    @RequestMapping (value="/frmBuscarAula.htm", method= RequestMethod.GET)
    public ModelAndView frmBuscarAula(){
        ModelAndView mv = new ModelAndView("frmBuscarAula");
        mv.addObject("BAula", new Aula());
        return mv;
    }
    @RequestMapping (value="/frmBuscarDocente.htm", method= RequestMethod.GET)
    public ModelAndView frmBuscarDocente(){
        ModelAndView mv = new ModelAndView("frmBuscarDocente");
        mv.addObject("BDocente", new Docente());
        return mv;
    }
    @RequestMapping (value="/frmBuscarSemestre.htm", method= RequestMethod.GET)
    public ModelAndView frmBuscarSemestre(){
        ModelAndView mv = new ModelAndView("frmBuscarSemestre");
        mv.addObject("BSemestre", new Semestre());
        return mv;
    }
    @RequestMapping (value="/frmBuscarCurso.htm", method= RequestMethod.GET)
    public ModelAndView frmBuscarCurso(){
        ModelAndView mv = new ModelAndView("frmBuscarCurso");
        mv.addObject("BCurso", new Curso());
        return mv;
    }
    @RequestMapping (value="/frmBuscarCarrera.htm", method= RequestMethod.GET)
    public ModelAndView frmBuscarCarrera(){
        ModelAndView mv = new ModelAndView("frmBuscarCarrera");
        mv.addObject("BCarrera", new Carrera());
        return mv;
    }
    
    
    //Procesar busquedas
    @RequestMapping(value = "/procesaBuscarAula.htm", method = RequestMethod.POST)
    public ModelAndView BuscarAula(@ModelAttribute ("BAula") Aula a) {
        ModelAndView mv = new ModelAndView("encontradoAula");
        Aula au = sadao.buscarAula(a.getCodAula());
        if(au == null)
            return new ModelAndView("error");
        else
            mv.addObject("aula",au);
        return mv;
    }
    @RequestMapping(value = "/procesaBuscarDocente.htm", method = RequestMethod.POST)
    public ModelAndView BuscarDocente(@ModelAttribute ("BDocente") Docente d) {
        ModelAndView mv = new ModelAndView("encontradoDocente");
        Docente doc = sddao.buscarDocente(d.getCodDocente());
        if(doc == null)
            return new ModelAndView("error");
        else
            mv.addObject("docente",doc);
        return mv;
    }
    @RequestMapping(value = "/procesaBuscarSemestre.htm", method = RequestMethod.POST)
    public ModelAndView BuscarSemestre(@ModelAttribute ("BSemestre") Semestre s) {
        ModelAndView mv = new ModelAndView("encontradoSemestre");
        Semestre sem = sdao.buscarSemestre(s.getCodSemestre());
        if(sem == null)
            return new ModelAndView("error");
        else{
            mv.addObject("semestre",sem);
            return mv;
        }
    }
    @RequestMapping(value = "/procesaBuscarCurso.htm", method = RequestMethod.POST)
    public ModelAndView BuscarCurso(@ModelAttribute ("BCurso") Curso c) {
        ModelAndView mv = new ModelAndView("encontradoCurso");
        Curso cur = cudao.buscarCurso(c.getCodCurso());
        if(cur == null)
            return new ModelAndView("error");
        else
            mv.addObject("curso",cur);
        return mv;
    }
    @RequestMapping(value = "/procesaBuscarCarrera.htm", method = RequestMethod.POST)
    public ModelAndView BuscarCarrera(@ModelAttribute ("BCarrera") Carrera c) {
        ModelAndView mv = new ModelAndView("encontradoCarrera");
        Carrera car = cadao.buscarCarrera(c.getCodCarrera());
        if(car == null)
            return new ModelAndView("error");
        else
            mv.addObject("carrera",car);
        return mv;
    }
    
    //Modificar
    @RequestMapping(value="/frmModificarAula.htm", method= RequestMethod.GET)
    public ModelAndView ModificarAula(@RequestParam int codigo){
        ModelAndView mv = new ModelAndView("frmModificarAula");
        Aula a = sadao.buscarAula(codigo);
        mv.addObject("IAula", a);
        return mv;
    }
    @RequestMapping(value="/frmModificarDocente.htm", method= RequestMethod.GET)
    public ModelAndView ModificarDocente(@RequestParam String codigo){
        ModelAndView mv = new ModelAndView("frmModificarDocente");
        Docente d = sddao.buscarDocente(codigo);
        mv.addObject("IDocente", d);
        return mv;
    }
    @RequestMapping(value="/frmModificarSemestre.htm", method= RequestMethod.GET)
    public ModelAndView ModificarSemestre(@RequestParam String codigo){
        ModelAndView mv = new ModelAndView("frmModificarSemestre");
        Semestre sem = sdao.buscarSemestre(codigo);
        mv.addObject("SemestreBB", sem);
        return mv;
    }
    @RequestMapping(value = "/frmModificarCurso.htm", method = RequestMethod.GET)
    public ModelAndView ModificarCurso(@RequestParam String codigo) {
        ModelAndView mv = new ModelAndView("frmModificarCurso");
        Curso c = cudao.buscarCurso(codigo);
        mv.addObject("cursoC", c);
        return mv;
    }

    @RequestMapping(value = "/frmModificarCarrera.htm", method = RequestMethod.GET)
    public ModelAndView frmModificarCarrera(@RequestParam String codigo) {
        ModelAndView mv = new ModelAndView("frmModificarCarrera");
        Carrera c = cadao.buscarCarrera(codigo);
        mv.addObject("carreraC", c);
        return mv;
    }
    @RequestMapping(value = "/frmModificarPlan.htm", method = RequestMethod.GET)
    public ModelAndView frmModificarPlan(@RequestParam int codigo) {
        ModelAndView mv = new ModelAndView("frmModificarPlan");
        Planes p = pdao.buscarPlan(codigo);
        mv.addObject("carreras", cadao.getListaCarrera());
        mv.addObject("planP", p);
        return mv;
    }
    
    //Eliminar
    @RequestMapping (value="/frmEliminarAula.htm", method= RequestMethod.GET)
    public String eliminarAula(@RequestParam int codigo){
        return (sadao.eliminarAula(codigo))?"exito":"error";
    }
    @RequestMapping (value="/frmEliminarDocente.htm", method= RequestMethod.GET)
    public String eliminarDocente(@RequestParam String codigo){
        return (sddao.eliminarDocente(codigo))?"exito":"error";
    }
    @RequestMapping(value = "/frmEliminarSemestre.htm", method = RequestMethod.GET)
    public String eliminarSemestre(@RequestParam String codigo) {
        return (sdao.eliminarSemestre(codigo))?"exito":"error";
    }
    @RequestMapping(value = "/frmEliminarCurso.htm", method = RequestMethod.GET)
    public String eliminarCurso(@RequestParam String codigo) {
        return (cudao.eliminarCurso(codigo))?"exito":"error";
    }
    @RequestMapping(value = "/frmEliminarCarrera.htm", method = RequestMethod.GET)
    public String eliminarCarrera(@RequestParam String codigo) {
        return (cadao.eliminarCarrera(codigo))?"exito":"error";
    }     
    @RequestMapping(value = "/frmEliminarPlan.htm", method = RequestMethod.GET)
    public String eliminarPlan(@RequestParam int codigo) {
        return (pdao.eliminarPlan(codigo))?"exito":"error";
    }
    //@RequestMapping (value="/frm_buscarSemestre.htm", method= RequestMethod.GET)
    
    
//         @RequestMapping(value = "/muestra_Semestre.htm", method = RequestMethod.POST)
//    public ModelAndView buscarSemestre(@ModelAttribute("txtcodigo")  String codigo ) {
//        ModelAndView mv = new ModelAndView("muestra_Semestre");
//        Semestre s = sdao.buscarSemestre(codigo);
//        if (s == null)
//        {
//            return new ModelAndView("error");
//        }
//        else{
//        mv.addObject("semestres", s);
//        return mv;}
//         }
            
    //public String BSemestre(){
    //    return "frm_buscarSemestre";
    //}
    
    @RequestMapping(value="/frmListarPlanPorCarrera.htm", method= RequestMethod.GET)
    public ModelAndView frmListarPlanesCarrera(){
        ModelAndView mv = new ModelAndView("frmListarPlanPorCarrera");
        mv.addObject("carreras", cadao.getListaCarrera());
        mv.addObject("planesP", new Carrera());
        return mv;
    }
    @RequestMapping(value="/busquedaPorCarrera.htm", method= RequestMethod.POST)
    public ModelAndView listarPlanesCarrera(@ModelAttribute ("planesP") Carrera c){
        ModelAndView mv = new ModelAndView("frmListarPlanPorCarrera");
        mv.addObject("carreras", cadao.getListaCarrera());
        mv.addObject("planes", pdao.getListaPlanesPorTipo(c));
        mv.addObject("planesP", new Carrera());
        return mv;
    }
    
    @RequestMapping(value="/frmVisualizarPlan.htm", method= RequestMethod.GET)
    public ModelAndView VisualizarPlan(@RequestParam int codigo){
        ModelAndView mv = new ModelAndView("visualizarPlan");
        mv.addObject("detalleplan", pdao.visualizarPlan(codigo));
        return mv;
    }
    
}

